﻿Imports System
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
''' <summary>
''' 用户的方案的设置
''' </summary>
''' <remarks></remarks>
<Serializable()> Public Class ProjSet
    Implements ICloneable


    Private m_ProjName As String
    Private m_Index As Integer
    Private m_ID As String
    Private m_Selected As Boolean = True
    ''' <summary>
    ''' 用于标记结果是否已经保存
    ''' </summary>
    ''' <remarks></remarks>
    Private m_IsSavedResult As Boolean = False
    ''' <summary>
    ''' 被选择的污染物的名称。如果没有相关的污染物，设置为""
    ''' </summary>
    ''' <remarks></remarks>
    Private m_PolluteName As String
    Private m_AerageingTime As New AerageingTime '平均时间选项
    Private m_GroupsCollection As New GroupsCollection  '污染源组
    Private m_ReceptorSelected As New ReceptorSelected  '网格和计算点
    Private m_TimeSetting As New TimeSetting
    Private m_ProjOutPutOption As New ProjOutPuts '输出选项类
    ''' <summary>
    ''' 用于储存方案的暂时Aermod文件。
    ''' </summary>
    ''' <remarks></remarks>
    Private m_TempAermod As New Aermod

    ''' <summary>
    ''' 被选择的污染物的名称。如果没有相关的污染物，设置为""
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property PolluteName() As String
        Get
            Return Me.m_PolluteName
        End Get
        Set(ByVal value As String)
            Me.m_PolluteName = value
        End Set
    End Property
    ''' <summary>
    ''' 方案的名称
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property ProjName() As String
        Get
            Return Me.m_ProjName
        End Get
        Set(ByVal value As String)
            Me.m_ProjName = value
        End Set
    End Property
    ''' <summary>
    ''' 用于储存方案的序号
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property Index() As Integer
        Get
            Return Me.m_Index
        End Get
        Set(ByVal value As Integer)
            Me.m_Index = value
        End Set
    End Property
    ''' <summary>
    ''' 方案的ID号，用于识别方案
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property ID() As String
        Get
            Return Me.m_ID
        End Get
        Set(ByVal value As String)
            Me.m_ID = value
        End Set
    End Property
    Property Selected() As Boolean
        Get
            Return Me.m_Selected
        End Get
        Set(ByVal value As Boolean)
            Me.m_Selected = value
        End Set
    End Property
    ''' <summary>
    ''' 平均时间选项
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property AerageingTime() As AerageingTime
        Get
            Return Me.m_AerageingTime
        End Get
        Set(ByVal value As AerageingTime)
            Me.m_AerageingTime = value
        End Set
    End Property
    ''' <summary>
    ''' 污染源组
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Property GroupsCollection() As GroupsCollection
        Get
            Return Me.m_GroupsCollection
        End Get
        Set(ByVal value As GroupsCollection)
            Me.m_GroupsCollection = value
        End Set
    End Property
    ''' <summary>
    ''' 网格和计算点
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property ReceptorSelected() As ReceptorSelected
        Get
            Return Me.m_ReceptorSelected
        End Get
        Set(ByVal value As ReceptorSelected)
            Me.m_ReceptorSelected = value
        End Set
    End Property

    ''' <summary>
    ''' 地面气象数据
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property TimeSetting() As TimeSetting
        Get
            Return Me.m_TimeSetting
        End Get
        Set(ByVal value As TimeSetting)
            Me.m_TimeSetting = value
        End Set
    End Property
    Public Property ProjOutPutOption() As ProjOutPuts
        Get
            Return Me.m_ProjOutPutOption
        End Get
        Set(ByVal value As ProjOutPuts)
            Me.m_ProjOutPutOption = value
        End Set
    End Property

    ''' <summary>
    ''' 用于储存方案的暂时Aermod文件。
    ''' </summary>
    ''' <remarks></remarks>
    Property TempAermod() As Aermod
        Get
            Return Me.m_TempAermod
        End Get
        Set(ByVal value As Aermod)
            Me.m_TempAermod = value
        End Set
    End Property
    ''' <summary>
    ''' 用于标记结果是否已经保存
    ''' </summary>
    ''' <remarks></remarks>
    Property IsSavedResult() As Boolean
        Get
            Return Me.m_IsSavedResult
        End Get
        Set(ByVal value As Boolean)
            Me.m_IsSavedResult = value
        End Set
    End Property
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="Aermod"></param>
    ''' <param name="FileDectory">用于存放小时变化文件的路径</param>
    ''' <remarks></remarks>
    Public Sub SetTempAermod(ByVal Aermod As Aermod, ByVal FileDectory As String)
        Me.m_TempAermod = Aermod.Clone '创建新的方案
        Me.m_TempAermod.Control.Run = False
        '根据方案中的污染物名称来修改Aermod的方案
        Me.m_TempAermod.Control.AllPolluts.SetPollute(Me.m_PolluteName)

        '根据方案中的平均时间来修改AERMOD的方案
        Me.m_TempAermod.Control.AerageingTime = Me.m_AerageingTime.Clone
        '修改嵌套网格点
        For isub As Integer = 0 To Me.m_TempAermod.Receptor.SubGridCart.Length - 1
            Me.m_TempAermod.Receptor.SubGridCart(isub).Selected = Me.m_ReceptorSelected.SubGridCartSlected(isub).Selected
        Next
        '修改关心点是否选中
        For i As Integer = 0 To Me.m_TempAermod.Receptor.DiscCarts.Length - 1
            Me.m_TempAermod.Receptor.DiscCarts(i).Selected = Me.m_ReceptorSelected.DiscCartsSlected(i).Selected
        Next
        '修改背景浓度
        For j As Integer = 0 To Me.m_TempAermod.BackCon.PollutBackCon.Count - 1
            Dim BackCon As New BackCon
            If Me.m_TempAermod.BackCon.PollutBackCon.Item(j).Name = Me.m_TempAermod.Control.AllPolluts.AllSelectPollut(0).Name Then
                BackCon.PollutBackCon.Add(Me.m_TempAermod.BackCon.PollutBackCon.Item(j).Clone)
                Me.m_TempAermod.BackCon = BackCon.Clone
                Exit For
            End If
        Next
        '修改区域背景浓度
        'Me.m_TempAermod.Receptor.RegionAeravageBackCon.SetPolluteCon(Me.m_TempAermod.Control.AllPolluts)
        '修改运行期间范围
        Me.m_TempAermod.MetSource.SurfFile.SetDataPeroid = Me.m_TimeSetting.SetDataPeroid
        If Me.m_TempAermod.MetSource.SurfFile.SetDataPeroid = True Then
            Me.m_TempAermod.MetSource.SurfFile.StartTime = Me.m_TimeSetting.StartTime
            Me.m_TempAermod.MetSource.SurfFile.EndTime = Me.m_TimeSetting.EndTime
            Me.m_TempAermod.MetSource.SurfFile.StartHour = Me.m_TimeSetting.StartHour
            Me.m_TempAermod.MetSource.SurfFile.EndHour = Me.m_TimeSetting.EndHour
            Me.m_TempAermod.MetSource.SurfFile.SetHour = Me.m_TimeSetting.SetHour
        End If
        '污染源小时变化文件要与气象数据中的时间一致
        Me.m_TempAermod.Source.StartDate = Me.m_TempAermod.MetSource.SurfFile.MetDataStartTime
        Me.m_TempAermod.Source.Enddate = Me.m_TempAermod.MetSource.SurfFile.MetDataEndTime
        Me.m_TempAermod.Source.FileDectory = FileDectory
        '修改污染源分组和污染源是否选中
        ReDim Me.m_TempAermod.Source.Groups.ArrayGroup(Me.m_GroupsCollection.Count - 1)
        For j As Integer = 0 To Me.m_GroupsCollection.Count - 1
            Me.m_TempAermod.Source.Groups.ArrayGroup(j) = Me.m_GroupsCollection.Item(j).Clone
        Next
        '污染源是否选中
        For i As Integer = 0 To Me.m_TempAermod.Source.PointSources.PointSource.Length - 1 '清空
            Me.m_TempAermod.Source.PointSources.PointSource(i).Seleted = False
        Next
        For i As Integer = 0 To Me.m_TempAermod.Source.AreaSources.AreaSource.Length - 1
            Me.m_TempAermod.Source.AreaSources.AreaSource(i).Seleted = False
        Next
        For i As Integer = 0 To Me.m_TempAermod.Source.AreaPolySources.AreaPolySource.Length - 1
            Me.m_TempAermod.Source.AreaPolySources.AreaPolySource(i).Seleted = False
        Next
        For i As Integer = 0 To Me.m_TempAermod.Source.AreacircSources.AreaCircSource.Length - 1
            Me.m_TempAermod.Source.AreacircSources.AreaCircSource(i).Seleted = False
        Next
        For i As Integer = 0 To Me.m_TempAermod.Source.VolumeSources.VolumeSource.Length - 1
            Me.m_TempAermod.Source.VolumeSources.VolumeSource(i).Seleted = False
        Next
        For i As Integer = 0 To Me.m_TempAermod.Source.OpenPitSources.OpenPitSource.Length - 1
            Me.m_TempAermod.Source.OpenPitSources.OpenPitSource(i).Seleted = False
        Next
        For i As Integer = 0 To Me.m_TempAermod.Source.LineSources.ArrayLineSources.Length - 1
            Me.m_TempAermod.Source.LineSources.ArrayLineSources(i).Seleted = False
        Next


        '污染源是否选中
        For i As Integer = 0 To Me.m_TempAermod.Source.PointSources.PointSource.Length - 1 '清空
            For j As Integer = 0 To Me.m_GroupsCollection.Count - 1
                For k As Integer = 0 To Me.m_GroupsCollection.Item(j).GroupSource.Length - 1
                    If Me.m_TempAermod.Source.PointSources.PointSource(i).Location.Srcid = Me.m_GroupsCollection.Item(j).GroupSource(k).SourceID And Me.m_GroupsCollection.Item(j).GroupSource(k).Selected = True Then
                        Me.m_TempAermod.Source.PointSources.PointSource(i).Seleted = True
                    End If
                Next

            Next
        Next
        For i As Integer = 0 To Me.m_TempAermod.Source.AreaSources.AreaSource.Length - 1
            For j As Integer = 0 To Me.m_GroupsCollection.Count - 1
                For k As Integer = 0 To Me.m_GroupsCollection.Item(j).GroupSource.Length - 1
                    If Me.m_TempAermod.Source.AreaSources.AreaSource(i).Location.Srcid = Me.m_GroupsCollection.Item(j).GroupSource(k).SourceID And Me.m_GroupsCollection.Item(j).GroupSource(k).Selected = True Then
                        Me.m_TempAermod.Source.AreaSources.AreaSource(i).Seleted = True
                    End If
                Next

            Next
        Next
        For i As Integer = 0 To Me.m_TempAermod.Source.AreaPolySources.AreaPolySource.Length - 1
            For j As Integer = 0 To Me.m_GroupsCollection.Count - 1
                For k As Integer = 0 To Me.m_GroupsCollection.Item(j).GroupSource.Length - 1
                    If Me.m_TempAermod.Source.AreaPolySources.AreaPolySource(i).Location.Srcid = Me.m_GroupsCollection.Item(j).GroupSource(k).SourceID And Me.m_GroupsCollection.Item(j).GroupSource(k).Selected = True Then
                        Me.m_TempAermod.Source.AreaPolySources.AreaPolySource(i).Seleted = True
                    End If
                Next

            Next
        Next
        For i As Integer = 0 To Me.m_TempAermod.Source.AreacircSources.AreaCircSource.Length - 1
            For j As Integer = 0 To Me.m_GroupsCollection.Count - 1
                For k As Integer = 0 To Me.m_GroupsCollection.Item(j).GroupSource.Length - 1
                    If Me.m_TempAermod.Source.AreacircSources.AreaCircSource(i).Location.Srcid = Me.m_GroupsCollection.Item(j).GroupSource(k).SourceID And Me.m_GroupsCollection.Item(j).GroupSource(k).Selected = True Then
                        Me.m_TempAermod.Source.AreacircSources.AreaCircSource(i).Seleted = True
                    End If
                Next

            Next
        Next
        For i As Integer = 0 To Me.m_TempAermod.Source.VolumeSources.VolumeSource.Length - 1
            For j As Integer = 0 To Me.m_GroupsCollection.Count - 1
                For k As Integer = 0 To Me.m_GroupsCollection.Item(j).GroupSource.Length - 1
                    If Me.m_TempAermod.Source.VolumeSources.VolumeSource(i).Location.Srcid = Me.m_GroupsCollection.Item(j).GroupSource(k).SourceID And Me.m_GroupsCollection.Item(j).GroupSource(k).Selected = True Then
                        Me.m_TempAermod.Source.VolumeSources.VolumeSource(i).Seleted = True
                    End If
                Next

            Next
        Next
        For i As Integer = 0 To Me.m_TempAermod.Source.OpenPitSources.OpenPitSource.Length - 1
            For j As Integer = 0 To Me.m_GroupsCollection.Count - 1
                For k As Integer = 0 To Me.m_GroupsCollection.Item(j).GroupSource.Length - 1
                    If Me.m_TempAermod.Source.OpenPitSources.OpenPitSource(i).Location.Srcid = Me.m_GroupsCollection.Item(j).GroupSource(k).SourceID And Me.m_GroupsCollection.Item(j).GroupSource(k).Selected = True Then
                        Me.m_TempAermod.Source.OpenPitSources.OpenPitSource(i).Seleted = True
                    End If
                Next

            Next
        Next
        For i As Integer = 0 To Me.m_TempAermod.Source.LineSources.ArrayLineSources.Length - 1
            For j As Integer = 0 To Me.m_GroupsCollection.Count - 1
                For k As Integer = 0 To Me.m_GroupsCollection.Item(j).GroupSource.Length - 1
                    If Me.m_TempAermod.Source.LineSources.ArrayLineSources(i).Location.Srcid = Me.m_GroupsCollection.Item(j).GroupSource(k).SourceID And Me.m_GroupsCollection.Item(j).GroupSource(k).Selected = True Then
                        Me.m_TempAermod.Source.LineSources.ArrayLineSources(i).Seleted = True
                    End If
                Next

            Next
        Next

        '修改输出选项
        Me.m_TempAermod.OutPutOption.Rectable = Me.m_ProjOutPutOption.Rectable.Clone '各点高值
        Me.m_TempAermod.OutPutOption.MaxTable = Me.m_ProjOutPutOption.MaxTable '所有点最大值
        Me.m_TempAermod.OutPutOption.GridMaxAnalisis = Me.m_ProjOutPutOption.GridMaxAnalisis '分析各点高值的浓度分布
        Me.m_TempAermod.OutPutOption.PostCareFile.IsPost = Me.m_ProjOutPutOption.PostCareFile.IsPost '输出所有组和所有时间的POST文件

        ReDim Me.m_TempAermod.OutPutOption.ArrayMaxiFile(Me.m_ProjOutPutOption.ArrayMaxiFile.Length - 1) '按用户的设置输出超过浓度限值的文件
        For j As Integer = 0 To Me.m_ProjOutPutOption.ArrayMaxiFile.Length - 1
            Me.m_TempAermod.OutPutOption.ArrayMaxiFile(j) = Me.m_ProjOutPutOption.ArrayMaxiFile(j).Clone
        Next
        ReDim Me.m_TempAermod.OutPutOption.ArrayPostFile(Me.m_ProjOutPutOption.ArrayPostFile.Length - 1) '按用户的设置输出POST文件
        For j As Integer = 0 To Me.m_ProjOutPutOption.ArrayPostFile.Length - 1
            Me.m_TempAermod.OutPutOption.ArrayPostFile(j) = Me.m_ProjOutPutOption.ArrayPostFile(j).Clone
        Next
    End Sub

    ''' <summary>
    ''' 接受点
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub InitalReceptorSelected(ByVal Receptor As Receptor)
        '关心点
        Dim myDisCarts(Receptor.DiscCarts.Length - 1) As DiscCart

        For i As Integer = 0 To Receptor.DiscCarts.Length - 1
            myDisCarts(i) = Receptor.DiscCarts(i).Clone
            myDisCarts(i).Selected = False
            For j As Integer = 0 To Me.m_ReceptorSelected.DiscCartsSlected.Length - 1
                If myDisCarts(i).ID = Me.m_ReceptorSelected.DiscCartsSlected(j).ID Then
                    myDisCarts(i).Selected = Me.m_ReceptorSelected.DiscCartsSlected(j).Selected
                End If
            Next
        Next
        '嵌套网格
        Dim mySubGrid(Receptor.SubGridCart.Length - 1) As GridCart
        For i As Integer = 0 To Receptor.SubGridCart.Length - 1
            mySubGrid(i) = Receptor.SubGridCart(i).Clone
            mySubGrid(i).Selected = False
            For j As Integer = 0 To Me.m_ReceptorSelected.SubGridCartSlected.Length - 1
                If mySubGrid(i).Netid = Me.m_ReceptorSelected.SubGridCartSlected(j).Netid Then
                    mySubGrid(i).Selected = Me.m_ReceptorSelected.SubGridCartSlected(j).Selected
                End If
            Next
        Next

        '赋值
        Me.m_ReceptorSelected = New ReceptorSelected
        Me.m_ReceptorSelected.DiscCartsSlected = myDisCarts
        Me.m_ReceptorSelected.SubGridCartSlected = mySubGrid

    End Sub

    Public Function Clone() As Object Implements System.ICloneable.Clone
        Dim obj As New ProjSet
        obj.m_ProjName = Me.m_ProjName
        obj.m_Index = Me.m_Index
        obj.m_ID = Me.m_ID
        obj.m_Selected = Me.m_Selected
       
        obj.m_PolluteName = Me.m_PolluteName
        obj.m_AerageingTime = Me.m_AerageingTime.Clone
        obj.m_GroupsCollection = Me.m_GroupsCollection.Clone  '污染源组
        obj.m_ReceptorSelected = Me.m_ReceptorSelected.Clone   '网格和计算点
        obj.m_TimeSetting = Me.m_TimeSetting.Clone
        obj.m_ProjOutPutOption = Me.m_ProjOutPutOption.Clone  '输出选项类
        If Me.m_TempAermod IsNot Nothing Then
            obj.m_TempAermod = Me.m_TempAermod.Clone
        Else
            obj.m_TempAermod = Nothing
        End If
        Return obj
    End Function
End Class
